package com.github.binarylei.zookeeper.watcher;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Test;

/**
 * @author: leigang
 * @version: 2018-04-05
 */
public class ConnectionWatcherTest {

    @Test
    public void test() throws KeeperException, InterruptedException {
        ConnectionWatcher zkWatcher = new ConnectionWatcher();
        zkWatcher.connect("127.0.0.1:2181");

        ZooKeeper zk = zkWatcher.getZk();
        zk.create("/date", String.valueOf(System.currentTimeMillis()).getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        //1. 注册watch事件
        zk.exists("/date", true);
        zk.setData("/date", String.valueOf(System.currentTimeMillis()).getBytes(), -1);

        //2. watch的触发是一次性的，要想再次触发必须重新注册
        zk.exists("/date", true);
        zk.setData("/date", String.valueOf(System.currentTimeMillis()).getBytes(), -1);

        zk.delete("/date", -1);
        zkWatcher.close();
    }
}
